package com.ftz.examinationsystem.controller;

import com.ftz.examinationsystem.pojo.Admin;
import com.ftz.examinationsystem.pojo.Room;
import com.ftz.examinationsystem.pojo.showinfo;
import com.ftz.examinationsystem.service.UserService;
import com.ftz.examinationsystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class URLController {
    @Autowired
    UserService userService;

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @GetMapping("/")
    public String login(){
        return "user/login";
    }

    @GetMapping("/register")
    public String register(){
        return "user/register";
    }

    //验证码
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }

    //登录
    @GetMapping("/login")
    @ResponseBody
    public JsonResult<Void> login(Admin admin, @RequestParam("captcha")String captcha, HttpServletRequest request, HttpSession session){
        System.out.println(admin);
        System.out.println("---------------");
        //验证码验证
        if (!CaptchaUtil.ver(captcha, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            return new JsonResult<>(400);
        }
        Admin admin1=userService.login(admin);
        System.out.println(admin1);
        if(admin1!=null){
            System.out.println("hello");
            session.setAttribute("userInfo",admin1);
            session.setAttribute("name",admin1.getUsername());
            return new JsonResult<>(200);
        }
        return new JsonResult<>(300);
    }

    //注册
    @GetMapping("/register1")
    @ResponseBody
    public JsonResult<Void> register(Admin admin){
        System.out.println(admin);
        int count = userService.selectByUsername(admin.getUsername());
        if(count==1){
            return new JsonResult<>(400,"用户名已经存在！");
        }
        int result = userService.inseruser(admin.getUsername(), admin.getPassword());
        if(result==1){
            return new JsonResult<>(200,"注册成功！");
        }else {
            return new JsonResult<>(400,"注册失败！");
        }
    }

    @GetMapping("/welcome")
    public String welcome(){
        return "welcome";
    }

    @GetMapping("student/add")
    public String add(){
        return "student/addStu";
    }

    @GetMapping("student/show")
    public String show(){
        return "student/showAll";
    }

    @GetMapping("room/add")
    public String addroom(){
        return "room/addRoom";
    }

    @GetMapping("room/list")
    public String roomlist(){
        return "room/RoomList";
    }

    @GetMapping("exam/unifiedArrage")
    public String unified(Model model,String course){
        List<Integer> integers = userService.countStus(course);
        List<String> roomAddr = userService.queryRoomAddr();
        List<Room> roomDetaile=userService.queryRoom();
        List<String> cname = userService.selectCourse();
        int sum=0,count=0;
        List<Integer> rooms = userService.countRooms();

        for(Integer i:integers){
            sum+=i;
        }
        for(Integer i:rooms){
            count+=i;
        }
        //跳转到分配设置页面并将机房名渲染到页面中
        model.addAttribute("roomAddr",roomAddr);  //存储机房地址
        model.addAttribute("room",roomDetaile);  //存储机房信息
        model.addAttribute("countStus",integers); //每个校区的人数
        model.addAttribute("totalStu",sum); //总人数
        model.addAttribute("countRoom",rooms); //每个校区机房数
        model.addAttribute("totalRoom",count); //机房总数
        model.addAttribute("cname",cname);//课程名
        return "exam/UnifiedArrange";
    }

    @GetMapping("exam/eDistribute")
    public String eDistribute(Model model){
        List<String> roomAddr = userService.queryRoomAddr();
        List<String> cname = userService.selectCourse();
        model.addAttribute("roomAddr",roomAddr); //机房地址
        model.addAttribute("cname",cname);//课程名
        return "exam/eDistribute";
    }

    @GetMapping("exam/stuDistribute")
    public String stuDistribute(Model model){
        List<String> roomAddr = userService.queryRoomAddr();
        List<String> cname = userService.selectCourse();
        model.addAttribute("roomAddr",roomAddr); //机房地址
        model.addAttribute("cname",cname);//课程名
        return "exam/stuDistribute";
    }

    @GetMapping("exam/IndividualArrage")
    public String IndividualArrage(Model model){
        List<String> roomAddr = userService.queryRoomAddr();
        List<String> cname = userService.selectCourse();
        model.addAttribute("cname",cname);//课程名
        model.addAttribute("roomAddr",roomAddr); //机房地址
        return "exam/IndividualArrage";
    }

    @GetMapping("exam/ClearRoomStu")
    public String ClearRoomStu(Model model){
        List<String> roomAddr = userService.queryRoomAddr();
        List<String> cname = userService.selectCourse();
        model.addAttribute("cname",cname);//课程名
        model.addAttribute("roomAddr",roomAddr); //机房地址
        return "exam/ClearRoomStu";
    }

    @GetMapping("exam/ChangeRoom")
    public String ChangeRoom(Model model){
        List<String> roomAddr = userService.queryRoomAddr();
        List<String> cname = userService.selectCourse();
        model.addAttribute("cname",cname);//课程名
        model.addAttribute("roomAddr",roomAddr); //机房地址
        return "exam/ChangeRoom";
    }
}
